#include "racket.h"
#include <QGraphicsPixmapItem>
#include<QObject>
#include <QGraphicsItemGroup>

Racket::Racket(QObject *parent1,QGraphicsItem *parent):
    QObject(parent1), QGraphicsItemGroup(parent)
{
        QPixmap NetPixmap(":/lng/picture/RacketNet.png");
        QPixmap StickPixmap(":/lng/picture/RacketStick.png");
        m_Net = new QGraphicsPixmapItem(NetPixmap, this);
        m_Stick = new QGraphicsPixmapItem(StickPixmap, this);

        // 设置球网和球拍杆子的相对位置，使其连接在一起
        m_Net->setPos(0, 0);
        m_Stick->setPos(m_Net->boundingRect().width()-15, m_Net->boundingRect().height());

        // 添加到组中
        addToGroup(m_Net);
        addToGroup(m_Stick);

        // 设置旋转点为球拍把手位置
        m_RotationPoint = m_Stick->pos() +
                        QPointF(m_Stick->boundingRect().width() / 2, m_Stick->boundingRect().height());
}


void Racket::SetStickPosition(const QPointF &pos)
{
    // 设置球拍把手位置
    setPos(pos - m_RotationPoint);
}

void Racket::RotateRacket(int angle)
{
    // 绕旋转点旋转球拍
    setTransformOriginPoint(m_RotationPoint);
    setRotation(angle);
}
